//
//  EndProjViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/12/10.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class EndProjViewController: PurchaseTableViewController {
    
    private var _ysbgs: [JSON] = []//验收报告
    var ysbgs: [JSON] {
        get{
            return _ysbgs
        }
        set{
            _ysbgs = newValue
        }
    }
    private var _others: [JSON] = [] //其他
    var others: [JSON] {
        get{
            return _others
        }
        set{
            _others = newValue
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return super.tableView(tableView, numberOfRowsInSection: section)
        case 1:
            return self.ysbgs.count//验收报告
        case 2:
            return self.documents.count//项目成果
        case 3:
            return self.others.count
        default:
            return self.others.count
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return super.tableView(tableView, heightForRowAt: indexPath)
        } else if indexPath.section == 1 {
            let name = self.ysbgs[indexPath.row].contents?["name"]?.rawString() ?? ""
            if let cell = tableView.cellForRow(at: indexPath) {
                let size = name.size(with: cell.textLabel!.font, maxSize: CGSize.init(width: self.view.width - 48, height: 10000))
                return size.height > 55 ? size.height  : 55
            }
        } else if indexPath.section == 2 {
            let name = self.documents[indexPath.row].contents?["name"]?.rawString() ?? ""
            if let cell = tableView.cellForRow(at: indexPath) {
                let size = name.size(with: cell.textLabel!.font, maxSize: CGSize.init(width: self.view.width - 48, height: 10000))
                return size.height > 55 ? size.height  : 55
            }
        } else if indexPath.section == 3 {
            let name = self.others[indexPath.row].contents?["name"]?.rawString() ?? ""
            if let cell = tableView.cellForRow(at: indexPath) {
                let size = name.size(with: cell.textLabel!.font, maxSize: CGSize.init(width: self.view.width - 48, height: 10000))
                return size.height > 55 ? size.height  : 55
            }
        }
        return 55
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            return super.tableView(tableView, cellForRowAt: indexPath)
        case 1:
            var cell = tableView.dequeueReusableCell(withIdentifier: "Attachment")
            
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "Attachment")
                cell?.textLabel?.numberOfLines = 0
            }
            
            cell?.selectionStyle = .none
            cell?.imageView?.image = UIImage(named: "icon_attachment")
            
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            
            cell?.textLabel?.textColor = UIColor.hex("458AE0")
            
            if indexPath.row < self.ysbgs.count {
                cell?.textLabel?.text = self.ysbgs[indexPath.row].contents?["name"]?.rawString()
            }
            
            return cell!
        case 2:
            var cell = tableView.dequeueReusableCell(withIdentifier: "Attachment")
            
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "Attachment")
                cell?.textLabel?.numberOfLines = 0
            }
            cell?.selectionStyle = .none
            cell?.imageView?.image = UIImage(named: "icon_attachment")
            
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            
            cell?.textLabel?.textColor = UIColor.hex("458AE0")
            
            if indexPath.row < documents.count {
                cell?.textLabel?.text = documents[indexPath.row].contents?["name"]?.rawString()
            }
            
            return cell!
        default:
            var cell = tableView.dequeueReusableCell(withIdentifier: "Attachment")
            
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "Attachment")
                cell?.textLabel?.numberOfLines = 0
            }
            cell?.selectionStyle = .none
            cell?.imageView?.image = UIImage(named: "icon_attachment")
            
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            
            cell?.textLabel?.textColor = UIColor.hex("458AE0")
            
            if indexPath.row < self.others.count {
                cell?.textLabel?.text = self.others[indexPath.row].contents?["name"]?.rawString()
            }
            
            return cell!
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.section {
        case 0:
            return
        default:
            
            let load = loadExcelViewController()
            self.filePath = .Project
            var sources: [JSON] = []
            if indexPath.section == 1 {
                sources = self.ysbgs
            } else if indexPath.section == 2 {
                sources = self.documents
            } else {
                sources = self.others
            }
            
            if let id = sources[indexPath.row].contents?["id"]?.rawString() {
                
                let p = Server + filePath.rawValue + "?id=" + id
                
                NetManager.download(path: p, prog: { (progress) in
                    SVProgressHUD.showProgress(Float(progress))
                }) { (file) in
                    
                    SVProgressHUD.dismiss()
                    
                    let previewURL = PreviewURL(url: file, name: sources[indexPath.row].contents?["name"]?.rawString())
                    
                    load.path = previewURL
                    
                    self.present(load, animated: true, completion: nil)
                }
            }
            
            return
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return 10
        default:
            return 40
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            return nil
        }
        let footer = UIView()
        
        footer.backgroundColor = UIColor.dftBack
        
        footer.backgroundColor = UIColor.white
        
        let titleLbl = UILabel()
        
        titleLbl.textColor = UIColor.textColor
        
        titleLbl.font = UIFont.systemFont(ofSize: 15)
        if section == 1 {
            titleLbl.text = "验收报告:"
        } else if section == 2 {
            titleLbl.text = "项目成果:"
        } else if section == 3 {
            titleLbl.text = "其他报告:"
        }
        
        titleLbl.frame = CGRect(origin: CGPoint(x: 15.0, y: 15.0), size: CGSize(width: titleLbl.contentSize.width, height: titleLbl.contentSize.height))
        
        footer.addSubview(titleLbl)
        
        return footer
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
}
